﻿using log4net;
using Nova_BLL;
using Nova_Common;
using Nova_Common.Extend;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.MesSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.MesSystem.Controllers
{
    /// <summary>
    /// 物料
    /// </summary>
    [RoutePrefix("api/MesSystem/Material")]
    public class MaterialController : BaseMesApiController
    {
        TBase_MaterialBll materialBll = new TBase_MaterialBll();
        ILog _logger = log4net.LogManager.GetLogger(typeof(MaterialController));

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("PushInfo")]
        [ValidateModel]
        [MesWebApiExceptionFilter]
        public MesApiResult PushInfo(MesMaterialInfoExtendModel param)
        {
            if (param.IsEmpty())
                return Error("物料信息参数不能为空!");
            _logger.InfoFormat("MES 推送物料信息,参数:{0}", param.ToJson());
            if (param.data.IsEmpty())
                return Error("物料信息data参数不能为空!");
            if (param.type == 0)
                return Insert(param.data.FirstOrDefault());
            else
                return Update(param.data.FirstOrDefault());
        }

        private MesApiResult Insert(MesMaterialInfoModel mat)
        {
            if (mat.IsEmpty())
                return Error("物料信息传递的参数为空!");
            var model = materialBll.GetByMatCode(mat.part_no);
            if (!model.IsEmpty())
                return Error("此物料编码已经存在库中，无法新增物料!");
            TBase_Material entity = new TBase_Material();
            entity.PID = Guid.NewGuid().ToString().ToUpper();
            entity.Mat_Code = mat.part_no;
            entity.Mat_Name = mat.part_name;
            entity.S01 = mat.spec;
            entity.TM01 = mat.material;
            entity.Unit_Base = mat.unit;
            entity.TM02 = mat.out_diam;
            entity.TM03 = mat.length;
            entity.TM04 = mat.part_status.ToString();
            entity.TM05 = mat.material_property.ToString();
            entity.MiniNum = string.IsNullOrWhiteSpace(mat.safety_inventory) ? 0 : decimal.Parse(mat.safety_inventory);
            entity.MiniTypeID = mat.part_type.ToString();
            entity.GroupType = mat.common;
            entity.S02 = mat.description;
            entity.CDT = DateExtend.GetTime(mat.create_time, true);
            entity.ABC_Kind = 1;
            entity.OID = "MES";
            bool isInsert = materialBll.InsertEntity(entity);
            return isInsert ? Success() : Error("新增物料信息失败!");
        }
        private MesApiResult Update(MesMaterialInfoModel mat)
        {
            if (mat.IsEmpty())
                return Error("物料信息传递的参数为空!");
            TBase_Material entity = materialBll.GetByMatCode(mat.part_no);
            if (!entity.IsEmpty())
            {
                entity.Mat_Code = mat.part_no;
                entity.Mat_Name = mat.part_name;
                entity.S01 = mat.spec;
                entity.TM01 = mat.material;
                entity.Unit_Base = mat.unit;
                entity.TM02 = mat.out_diam;
                entity.TM03 = mat.length;
                entity.TM04 = mat.part_status.ToString();
                entity.TM05 = mat.material_property.ToString();
                entity.MiniNum = string.IsNullOrWhiteSpace(mat.safety_inventory) ? 0 : decimal.Parse(mat.safety_inventory);
                entity.TM06 = mat.common.ToString();
                entity.GroupType = mat.common;
                entity.MiniTypeID = mat.part_type.ToString();
                entity.S02 = mat.description;
                entity.CDT = DateExtend.GetTime(mat.create_time, true);
                bool isUpdate = materialBll.UpdateEntity(entity);
                return isUpdate ? Success() : Error("修改物料信息失败!");
            }
            else
                return Error(string.Format("物料编码part_no:{0}不存在与库中,无法修改!", mat.part_no));
        }
    }

}
